//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<shifter.h>
///	@path	~/src/gui/
///	@date	2008/06/22
///	@desc	Parameter shifter.

#pragma once

#include "xeres.h"

#include "lib/math/math_ext.h"

namespace xeres {

	/*! 
		\class	AttrShifter
		\brief	Used for shifting parameters.
	*/
	class AttrShifter
	{
	public:

		/// \ctor
		AttrShifter( float param , float variation )
			: m_param( param )
			, m_dest( param )
			, m_variation( variation )
		{
			XS_ASSERT( m_variation > 0 );
		}

		/// \ctor
		AttrShifter( const AttrShifter& rhs )
			: m_param( rhs.m_param )
			, m_dest( rhs.m_dest )
			, m_variation( rhs.m_variation )
		{
			XS_ASSERT( m_variation > 0 );
		}

		/// \brief	Get param.
		float Get( void )
		{
			return m_param;
		}

		/// \brief	Set param.
		void Set( float param , bool shift = true )
		{
			if( shift )
			{
				// do shift, from current param to dest
				m_dest = param;
			}
			else
			{
				// do not perform shift
				m_dest = m_param = param;
			}
		}

		/// \brief	Set param only.
		void SetParam( float param )
		{
			m_param = param;
		}

		/// \brief	Set destination only.
		void SetDest( float dest )
		{
			m_dest = dest;
		}

		/// \brief	Ended?
		bool IsEnded( void ) const
		{
			return m_param == m_dest;
		}

		/// \brief	Tick update.
		void Tick( float dTime )
		{
			if( m_param < m_dest )
				m_param = min( m_param + m_variation * dTime , m_dest );
			else
				m_param = max( m_param - m_variation * dTime , m_dest );
		}

	private:

		//@ data

		// current parameter value
		float	m_param;
		// parameter destination
		float	m_dest;
		// variation per second
		float	m_variation;
	};

} // namespace xeres
